<template>
  <el-container class="template-page">
    <el-header>
      <div class="left-panel">
        <el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input v-model="search.channelName" placeholder="名称" clearable></el-input>
          <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <jdTable ref="table" :apiObj="apiObj" size="large" @selection-change="selectionChange" :params="params" stripe>
        <!-- <el-table-column type="selection" align="center" width="50"></el-table-column> -->
        <el-table-column label="ID" prop="id" align="center" width="80"></el-table-column>
		<el-table-column label="名称" prop="channelName" width="200"></el-table-column>
        <el-table-column label="发送渠道" prop="channelType" width="200">
			<template #default="scope">
				<DictTag :options="message_type" :value="scope.row.channelType"></DictTag>
			</template>
		</el-table-column>
        <el-table-column label="配置内容" prop="channelConfig" min-width="200" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="200">
          <template #default="scope">
            <el-button-group>
              <el-button text type="primary" @click="templateModify(scope.row, scope.$index)" class="button">编辑</el-button>
              <el-popconfirm title="确定要删除吗？" @confirm="templateDelete(scope.row, scope.$index)">
                <template #reference>
                  <el-button text type="primary" class="button">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </jdTable>
    </el-main>
  </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save = false"></save-dialog>
</template>

<script>
import saveDialog from "./save"
export default {
  components: {
	  saveDialog
  },
  data() {
    return {
      selection: [],
      search: {
        templateName: ""
      },
      apiObj: this.$API.system.template.channelList,
      params: {},
      dialog: {
        save: false
      },
      message_type: []
    }
  },
  mounted() {
    this.getDict()
  },
  methods: {
    //添加
    add() {
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open()
      })
    },
    //编辑
    templateModify(row) {
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row)
      })
    },
    //删除
    async templateDelete(row) {
      var res = await this.$API.system.template.delTemplate.delete(row.id)
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("删除成功")
      } else {
        this.$alert(res.msg, "提示", { type: "error" })
      }
    },
    // 获取通知方式字典
    async getDict() {
      this.message_type = await this.Dict.get("sys_message_model")
    },
    // 搜索
    upsearch() {
      this.params = { ...this.search }
    },
    // 批量选择
    selectionChange(value) {
      this.selection = value
    },
    // 监听成功回调
    handleSaveSuccess() {
      this.$refs.table.refresh()
    }
  }
}
</script>

<style lang="scss" scoped></style>
